/******************************************************************************
 *
 * @file       userdata.h
 * @brief      用户搜索的结果用户信息
 *
 * @author     ario
 * @date       2024/08/09
 * @history
 *****************************************************************************/

#ifndef USERDATA_H
#define USERDATA_H

#include <QJsonArray>
#include <QJsonObject>
#include <QString>
#include <memory>
#include <vector>

// 联系人搜索结果的用户信息
class SearchInfo
{
public:
    SearchInfo(int uid, QString name, QString nick, QString desc, QString icon, int sex)
        : _uid(uid), _name(name), _nick(nick), _desc(desc), _icon(icon), _sex(sex)
    {
    }

    int _uid;
    QString _name;
    QString _nick;
    QString _desc;
    QString _icon;
    int _sex;
};

struct AuthInfo
{
    AuthInfo(int uid, QString name, QString nick, QString icon, int sex) : _uid(uid), _name(name), _nick(nick), _icon(icon), _sex(sex) {}

    int _uid;
    QString _name;
    QString _nick;
    QString _icon;
    int _sex;
};

struct AuthRsp
{
    AuthRsp(int peer_uid, QString peer_name, QString peer_nick, QString peer_icon, int peer_sex)
        : _uid(peer_uid), _name(peer_name), _nick(peer_nick), _icon(peer_icon), _sex(peer_sex)
    {
    }

    int _uid;
    QString _name;
    QString _nick;
    QString _icon;
    int _sex;
};

struct TextChatData;
struct FriendInfo
{
    FriendInfo(int uid, QString name, QString nick, QString icon, int sex, QString desc, QString back, QString last_msg = "")
        : _uid(uid), _name(name), _nick(nick), _icon(icon), _sex(sex), _desc(desc), _back(back), _last_msg(last_msg)
    {
    }

    FriendInfo(std::shared_ptr<AuthInfo> auth_info)
        : _uid(auth_info->_uid), _name(auth_info->_name), _nick(auth_info->_nick), _icon(auth_info->_icon), _sex(auth_info->_sex)
    {
    }

    FriendInfo(std::shared_ptr<AuthRsp> auth_rsp)
        : _uid(auth_rsp->_uid), _name(auth_rsp->_name), _nick(auth_rsp->_nick), _icon(auth_rsp->_icon), _sex(auth_rsp->_sex)
    {
    }

    void AppendChatMsgs(const std::vector<std::shared_ptr<TextChatData>> text_vec)
    {
        for(const auto & text: text_vec){
          _chat_msgs.push_back(text);
        }
    }

    int _uid;
    QString _name;
    QString _nick;
    QString _icon;
    int _sex;
    QString _desc;
    QString _back;
    QString _last_msg;
    std::vector<std::shared_ptr<TextChatData>> _chat_msgs;
};

struct UserInfo
{
    UserInfo(int uid, QString name, QString nick, QString icon, int sex, QString last_msg = "")
        : _uid(uid), _name(name), _nick(nick), _icon(icon), _sex(sex), _last_msg(last_msg)
    {
    }

    UserInfo(std::shared_ptr<AuthInfo> auth) : _uid(auth->_uid), _name(auth->_name), _nick(auth->_nick), _icon(auth->_icon), _sex(auth->_sex), _last_msg("") {}

    UserInfo(int uid, QString name, QString icon) : _uid(uid), _name(name), _nick(_name), _icon(icon), _sex(0), _last_msg("") {}

    UserInfo(std::shared_ptr<AuthRsp> auth) : _uid(auth->_uid), _name(auth->_name), _nick(auth->_nick), _icon(auth->_icon), _sex(auth->_sex), _last_msg("") {}

    UserInfo(std::shared_ptr<SearchInfo> search_info)
        : _uid(search_info->_uid), _name(search_info->_name), _nick(search_info->_nick), _icon(search_info->_icon), _sex(search_info->_sex), _last_msg("")
    {
    }

    UserInfo(std::shared_ptr<FriendInfo> friend_info)
        : _uid(friend_info->_uid), _name(friend_info->_name), _nick(friend_info->_nick), _icon(friend_info->_icon), _sex(friend_info->_sex), _last_msg("")
    {
        _chat_msgs = friend_info->_chat_msgs;
    }

    int _uid;
    QString _name;
    QString _nick;
    QString _icon;
    int _sex;
    QString _last_msg; // 在聊天界面显示最近的一条聊天信息
    std::vector<std::shared_ptr<TextChatData>> _chat_msgs;
};

struct TextChatData
{
    TextChatData(QString msg_id, QString msg_content, int fromuid, int touid) : _msg_id(msg_id), _msg_content(msg_content), _from_uid(fromuid), _to_uid(touid)
    {
    }
    QString _msg_id;
    QString _msg_content;
    int _from_uid;
    int _to_uid;
};

struct TextChatMsg
{
    TextChatMsg(int fromuid, int touid, QJsonArray arrays) : _to_uid(touid), _from_uid(fromuid)
    {
        for (auto msg_data : arrays) {
            auto msg_obj = msg_data.toObject();
            auto content = msg_obj["content"].toString();
            auto msgid = msg_obj["msgid"].toString();
            auto msg_ptr = std::make_shared<TextChatData>(msgid, content, fromuid, touid);
            _chat_msgs.push_back(msg_ptr);
        }
    }
    int _to_uid;
    int _from_uid;
    std::vector<std::shared_ptr<TextChatData>> _chat_msgs;
};

class AddFriendApply
{
public:
    AddFriendApply(int from_uid, QString name, QString desc, QString icon, QString nick, int sex)
        : _from_uid(from_uid), _name(name), _desc(desc), _icon(icon), _nick(nick), _sex(sex) {}
    int _from_uid;
    QString _name;
    QString _desc;
    QString _icon;
    QString _nick;
    int _sex;
};

struct ApplyInfo
{
    ApplyInfo(int uid, QString name, QString desc, QString icon, QString nick, int sex, int status)
        : _uid(uid), _name(name), _desc(desc), _icon(icon), _nick(nick), _sex(sex), _status(status)
    {
    }

    ApplyInfo(std::shared_ptr<AddFriendApply> addinfo)
        : _uid(addinfo->_from_uid), _name(addinfo->_name), _desc(addinfo->_desc), _icon(addinfo->_icon), _nick(addinfo->_nick), _sex(addinfo->_sex), _status(0)
    {
    }
    void SetIcon(QString head)
    {
        _icon = head;
    }
    int _uid;
    QString _name;
    QString _desc;
    QString _icon;
    QString _nick;
    int _sex;
    int _status;
};

#endif  // USERDATA_H
